package com.cv.platform.framework.es.core;

import org.springframework.data.elasticsearch.core.mapping.PropertyValueConverter;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * Elasticsearch LocalDateTime 转换器
 *
 * @author Charles_XDXD
 */
public class EsLocalDateTimeConverter implements PropertyValueConverter {

    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");

    @Override
    public Object write(Object value) {
        if (value instanceof LocalDateTime) {
            return ((LocalDateTime) value).format(formatter);
        }
        return value;
    }

    @Override
    public Object read(Object value) {
        if (value instanceof String) {
            return LocalDateTime.parse((String) value, formatter);
        }
        return value;
    }

}
